package com.tsyun.view_project;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class WebViewActivity extends AppCompatActivity {

    //定义WebView对象
    private WebView webView;
    //显示网页的标题的文本
    private TextView tvTitle;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        //初始化webview对象
        webView = findViewById(R.id.webview);
        tvTitle = findViewById(R.id.tv_web_title);
        //获取浏览器的设置
        //加载网络上的网页
//        webView.loadUrl("https://www.baidu.com/");
//        webView.loadUrl("https://www.sina.com.cn/");
        //加载本地的网页资源文件
//        webView.loadUrl("file:///android_asset/demo.html");
        webView.loadData(""
                ,"text/html","UTF-8");
        //文本框再点击的时候获取焦点
        webView.requestFocusFromTouch();
        //获取webview的设置
        WebSettings settings = webView.getSettings();
        //设置webview支持手势缩放
        settings.setSupportZoom(true);
        //设置webview显示控制条
        settings.setBuiltInZoomControls(true);
        //设置允许使用JavaScript
        settings.setJavaScriptEnabled(true);
        //通过设置webchromeClient来获取网页的标题 图标 加载进度等
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                tvTitle.setText(title);
            }

            //获取网页的图标
            @Override
            public void onReceivedIcon(WebView view, Bitmap icon) {
                super.onReceivedIcon(view, icon);
            }

            //获取网页加载进度
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
            }
        });
        //阻止跳转到手机第三方浏览器进行浏览 仅在本应用进行网页的浏览
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            /**
             *
             * @param view
             * @param url 在网页中点击的url地址
             * @return
             */
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, request);
            }
        });
    }

    //监听返回按钮的事件
    @Override
    public void onBackPressed() {
        //如果浏览器可以回退，则回退到之前的页面 否则结束当前界面
        if (webView!=null && webView.canGoBack()){
            webView.goBack();
        }else {
            super.onBackPressed();
        }
    }
}
